<template>
  <div class="container">
    <div class="title">修改密码</div>
    <div class="main">
      <el-form :model="form" :rules="rules">
        <el-form-item label="请选择验证身份方式：" label-width="180px">
          <el-checkbox-group v-model="form.verification" :min="1" :max="1">
            <el-checkbox label="密码验证" value="password" />
            <el-checkbox label="邮箱验证" value="email" />
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="原密码：" label-width="180px" prop="oldPassword">
          <el-input v-model="form.oldPassword"></el-input>
        </el-form-item>
        <el-form-item label="新密码：" label-width="180px" prop="newPassword">
          <el-input v-model="form.newPassword"></el-input>
        </el-form-item>
        <el-form-item
          label="重复新密码："
          label-width="180px"
          prop="confirmPwd"
        >
          <el-input v-model="form.confirmPwd"></el-input>
        </el-form-item>
        <el-form-item label="验证码：" label-width="180px">
          <el-input v-model="form.verificationCode"></el-input>
        </el-form-item>
        <el-form-item label-width="180px">
          <el-button type="danger" @click="onSubmit">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts">
import { ElMessage } from 'element-plus/lib/components';
import { defineComponent, reactive, toRefs } from 'vue';

export default defineComponent({
  setup() {
    const state = reactive({
      form: {
        verification: [],
        oldPassword: '',
        newPassword: '',
        confirmPwd: '',
        verificationCode: '666666',
      },
    });

    var validatePass2 = (rule: any, value: any, callback: any) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== state.form.newPassword) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };

    const rules = {
      oldPassword: [
        {
          required: true,
          message: '请输入旧密码',
          trigger: 'blur',
        },
      ],
      newPassword: [
        {
          required: true,
          message: '请输入新密码',
          trigger: 'blur',
        },
        {
          min: 6,
          max: 16,
          message: '密码长度必须在6-16个字符之间',
        },
        {
          pattern: /^\d+[A-Za-z]+$/,
          message: '密码必须包含字母及数字',
        },
      ],
      confirmPwd: [
        {
          validator: validatePass2,
          trigger: 'blur',
          required: true,
        },
      ],
    };

    return {
      ...toRefs(state),
      rules,
    };
  },
  methods: {
    onSubmit() {
      ElMessage({
        type: 'success',
        message: '密码修改成功！',
      });
    },
  },
});
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  padding: 62px 33px;
  border: 1px solid #ddd;

  .title {
    border-bottom: 1px solid #ddd;
    font-size: 18px;
    padding-bottom: 20px;
  }
  .main {
    margin: 20px 30px;
  }
}

::v-deep .el-form-item__content {
  max-width: 320px;
}
</style>
